import pygame,sys
import set
from pygame.locals import *
from config_set import CONFIG

class Button(pygame.sprite.Sprite):
    #按钮类，初始化参数包括，原始的图片，鼠标放上去显示的图片，图片的位置，被点击后要处理的函数，是否要设置图片的大小，以及图片的尺寸和颜色键
    def __init__(self,normal_image,hover_image,position,callback,set_size=None,size=(100,100),colorkey=True):
        pygame.sprite.Sprite.__init__(self)
        self.normal_image,self.rect=set.load_pic(normal_image,set_size,colorkey,size)
        self.hover_image,_=set.load_pic(hover_image,set_size,colorkey,size)
        self.image=self.normal_image
        self.rect.center=position
        self.callback= callback
        self.is_hover=False
    
    #这个是判断鼠标的状态，鼠标放上去，显示鼠标放上去的图片，鼠标点击，处理被点击后的函数
    def handle_event(self,event):
        if event.type==MOUSEMOTION:
            self.is_hover=self.rect.collidepoint(event.pos)
        elif event.type==MOUSEBUTTONDOWN and self.is_hover:
            if self.is_hover and event.button==1:
                self.callback()
                
        self.image = self.hover_image if self.is_hover else self.normal_image
        
#菜单类
    
class Menu:
    #初始化参数是将菜单放到哪里
    def __init__(self,screen):
        self.screen=screen
        self.buttons = pygame.sprite.Group()
    #添加按键
    def add_button(self,button):
        self.buttons.add(button)
    #绘制按钮
    def draw(self):
        self.buttons.draw(self.screen)
    #处理每一个按钮的事件。
    def handle_event(self,events):
        for event in events:
            if event.type in (pygame.MOUSEMOTION, pygame.MOUSEBUTTONDOWN):
                for button in self.buttons:
                    button.handle_event(event)
#计时器，每隔time毫秒输出一次True                    
class Timer():
    def __init__(self,time):
        self.time = time
        self.last_time = pygame.time.get_ticks()
    def is_seted(self):
        now = pygame.time.get_ticks()
        if now - self.last_time > self.time:
            self.last_time = now
            return True
        return False
            
#玩家类
class Player(pygame.sprite.Sprite):
    #变量为位置，图片，是否设置大小，图片大小,子弹所在的精灵组,以及所有精灵组
    #里面包含速度，血量
    def __init__(self,position,image,set_size=None,size=(100,100),bullet_group=None,all_sprites=None):
        pygame.sprite.Sprite.__init__(self)
        self.bullet_group = bullet_group
        self.all_sprites = all_sprites
        self.image,self.rect=set.load_pic(image,set_size=set_size,size=size)
        self.rect.center=position
        self.speed=5
        self.hp = 1
        self.max_hp = 1
        self.hp_bar = 10
        self.shoot_timer = 200
        self.last_shoot = 0
        self.pos = pygame.Vector2(self.rect.center)
    #加载子弹
    def spawn_shoot(self):
        now = pygame.time.get_ticks()
        if now - self.last_shoot > self.shoot_timer:
            bullet = Bullet_player(self.rect.center,CONFIG['player_bullet'],set_size=True,size=(10,20),speed=10,damage=1)
            bullet.add(self.bullet_group,self.all_sprites)
            self.last_shoot = now
    #发射子弹
    def shoot(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_SPACE]:
            self.spawn_shoot()
    #按键移动
    def move(self):
        self.limit()
        keys = pygame.key.get_pressed()
        dx  = (keys[K_d] -keys[K_a])*self.speed
        dy  = (keys[K_s] -keys[K_w])*self.speed
        self.pos.x += dx
        self.pos.y += dy
        self.rect.center = self.pos
    #限制，不能超出屏幕
    def limit(self):
        self.pos.x = max(0,min(self.pos.x,CONFIG['screen_size'][0]))
        self.pos.y = max(0,min(self.pos.y,CONFIG['screen_size'][1]))
    #处理伤害
    def take_damage(self,damage):
        self.hp -= damage
        if self.hp <= 0:
            self.kill()
    #展示血量
    def show_hp(self,surface):
        set.draw_text(f'HP:{self.hp}',CONFIG['font1'],60,(255,255,255),surface,60,60)
    #检查是否存活
    def is_alive(self):
        if self.hp <= 0:
            return False
    def update(self):
    
        self.move()
        self.shoot()
    #敌人类   
class Enemy(pygame.sprite.Sprite):
    def __init__(self,position,image,set_size=None,size=(100,100),bullet_group=None,all_sprites=None,on_death=None):
        pygame.sprite.Sprite.__init__(self)
        self.bullet_group = bullet_group
        self.all_sprites = all_sprites
        self.on_death = on_death
        self.image,self.rect=set.load_pic(image,set_size=set_size,size=size)
        self.rect.center=position
        self.speed=2
        self.shoot_timer =Timer(1000)#计时器
        self.pos = pygame.Vector2(self.rect.center)
        self.hp_bar = 10
        self.max_hp = 5
        self.hp = 5
        self.damage= 5 
    #发射子弹
    def shoot(self):
        if self.shoot_timer.is_seted():
            if self.groups():
                bullet = Bullet_enemy(self.rect.center,CONFIG['enemy_bullet'],set_size=True,size=(10,20),speed=5,damage=1)
                bullet.add(self.bullet_group,self.all_sprites)
    #向下移动
    def move(self):
        self.limit()
        self.rect.y += self.speed
    #移动限制
    def limit(self):
        if self.rect.y > CONFIG['screen_size'][1]:
            self.kill()
    #处理伤害
    def take_damage(self,damage):
        self.hp -= damage
        if self.hp <= 0:
            if self.on_death:
                self.on_death()
            self.kill()

    def update(self):
        self.move()
        self.shoot()
    
#子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self,position,image,set_size=None,size=(10,30),speed=5,damage=1):
        pygame.sprite.Sprite.__init__(self)
        self.image,self.rect = set.load_pic(image,set_size,size=size)
        self.rect.center = position
        self.speed = speed
        self.damage = damage
    
#玩家子弹
class Bullet_player(Bullet):
    def __init__(self,position,image,set_size=None,size=(10,30),speed=5,damage=1):
        super().__init__(position,image,set_size,size,speed,damage)
        
    def move(self):
        self.rect.y -= self.speed 
        if self.rect.y<0:
            self.kill()
    def update(self):
        self.move()
#敌人子弹
class Bullet_enemy(Bullet):
    def __init__(self,position,image,set_size=None,size=(10,30),speed=5,damage=1):
        super().__init__(position,image,set_size,size,speed,damage)
    def move(self):
        self.rect.y += self.speed 
        if self.rect.y>CONFIG['screen_size'][1]:
            self.kill()
    def update(self):
        self.move()